package cn.tjd.smart_health.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.tjd.smart_health.api.CheckGroupService;
import cn.tjd.smart_health.common.constant.MessageConstant;
import cn.tjd.smart_health.common.entity.CheckGroup;
import cn.tjd.smart_health.common.pojo.PageSelect;
import cn.tjd.smart_health.common.pojo.QueryBean;
import cn.tjd.smart_health.common.util.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/checkgroup")
@RestController
public class CheckGroupController {

    @DubboReference
    private CheckGroupService checkGroupService;

    @PostMapping ("/findPage")
    public PageSelect findPage(@RequestBody QueryBean queryBean){
        return checkGroupService.findPage(queryBean.getCurrentPage(),queryBean.getPageSize(),queryBean.getQueryString());
    }

    @GetMapping("/findAll")
    public R findAll(){
        try{
           List data = checkGroupService.findAll();
           return new R(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,data);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @PostMapping ("/add")
    public R addGroup(@RequestBody CheckGroup checkGroup,
                            @RequestParam("checkItemIds") Integer[] checkItemIds){
        try{
           Integer result = checkGroupService.addCheckGroup(checkGroup,checkItemIds);
           return new R(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    @GetMapping("/delete")
    public R deleteGroup(@RequestParam("id") Integer id){
        try{
            Integer result = checkGroupService.deleteCheckGroup(id);
            return new R(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    @GetMapping("/findById")
    public R findById(@RequestParam("id") Integer id){
        CheckGroup checkGroup = checkGroupService.findById(id);
       if (!ObjectUtil.isEmpty(checkGroup)){
           return new R(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
       }

       return new R(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public R findCheckItemIdsByCheckGroupId(@RequestParam("id") Integer id){
        try{
            List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new R(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItemIds);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    @PostMapping("/edit")
    public R editGroup(@RequestBody CheckGroup checkGroup,
                            @RequestParam("checkItemIds") Integer[] checkItemIds){

        try {
            Integer result = checkGroupService.updateCheckGroup(checkGroup,checkItemIds);
            return new R(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new R(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }



}
